#!/usr/bin/env python
# coding:utf-8
import sys
import os
import json
import random
import time
import hashlib
import logging
import requests


def str_dict_utf8(dict_obj):
	return json.dumps(dict_obj).decode('unicode-escape')


def pretty_dict(obj, indent=' '):
	def _pretty(obj, indent):
		for i, tup in enumerate(obj.items()):
			k, v = tup
			
			# 如果是字符串则拼上""
			if isinstance(k, basestring): k = '"%s"' % k
			if isinstance(v, basestring): v = '"%s"' % v
			
			# 如果是字典则递归
			
			if isinstance(v, dict):
				v = ''.join(_pretty(v, indent + ' ' * len(str(k) + ': {')))  # 计算下一层的indent
			
			# case,根据(k,v)对在哪个位置确定拼接什么
			if i == 0:  # 开头,拼左花括号
				if len(obj) == 1:
					yield '{%s: %s}' % (k, v)
				else:
					yield '{%s: %s,\n' % (k, v)
			elif i == len(obj) - 1:  # 结尾,拼右花括号
				yield '%s%s: %s}' % (indent, k, v)
			else:  # 中间
				yield '%s%s: %s,\n' % (indent, k, v)
	
	print ''.join(_pretty(obj, indent))


def get_md5(s):
	# 生成一个md5对象
	m1 = hashlib.md5()
	# 使用md5对象里的update方法md5转换
	m1.update(s.encode("utf-8"))
	md5str = m1.hexdigest()
	return md5str


def nodata2null(mystr):
	if str(mystr) == "no data":
		return ""
	return mystr


def str2int(mystr):
	return int(mystr)


# return [ [time, name, title] ]
def get_bos_file(file_path):
	files_arr = []
	with open(file_path, 'r') as f:
		# 文件格式如下
		#   2019-01-05 16:22:15       321154421     STANDARD  20161123_all about that bass_杨帆+阎妍+韩旭.mp4
		lines = f.readlines()
		for line in lines:
			line = line.rstrip('\n')  # 去掉结尾的回车符
			ftime = line[52:60]
			fname = line[52:]
			ftitle = line[61:-4]
			
			if True == ftime.isdigit():
				ftime = int(time.mktime(time.strptime(ftime, "%Y%m%d")))
			else:
				ftime = int(time.time())
				ftitle = line[52:-4]
			
			arr = [ftime, fname, ftitle]
			files_arr.append(arr)
	
	return files_arr

'''
{"singer": "张欢",
 "name": "十万毫升泪水",
 "song": "20160617_十万毫升泪水_张欢_深圳大学",
 "school": "深圳大学",
 "date": "20160617",
 "original": "蔡健雅"}
'''
def read_title_info(file_path):
	title_info = []
	with open(file_path, 'r') as f1:
		filelines = f1.xreadlines()

		for line in filelines:
			items = line.split(',')
			if 5 > len(items):
				print '===== file error! %s ======' % line

			#分割日期
			dates = items[0].split('/')
			date = '%s%02d%02d' % (dates[0], int(dates[1]), int(dates[2]))
			items = [unicode(i, 'utf-8') for i in items]
			key_item = {'date': date, 'name': items[1], 'original': items[2], 'singer': items[3], 'school': items[4]}
			song_name = '%s_%s' % (key_item['date'], key_item['name'])
			if key_item['singer'] != '':    song_name += '_%s' % key_item['singer']
			if key_item['school'] != '':    song_name += '_%s' % key_item['school']
			song_name = song_name.replace('/', '+')
			song_name = song_name.replace('\\', '+')
			song_name = song_name.replace('&', '+')
			song_name = song_name.replace('?', ' ')
			key_item['song'] = song_name

#			for k,v in key_item.items():
#				print '%s=%s' % (k,v)
			
			title_info.append(key_item)

		# end for line in filelines
	#end with open(file_path, 'r') as f1:

	return title_info

'''
翻唱：深圳大学 张欢 《十万毫升泪水》（cover 蔡健雅）
原创：集美大学 孙浩 《再见不再见》
'''
def change_file_title(file_info, ori_info):
	t1 = file_info[1][:-4]
#	t1 = unicode(t1, 'utf-8')
	temp = t1.split('_') # 算什么男人_张云霄+陈嘉杰_四川音乐学院+韩山师范学院
	temp = [unicode(i, 'utf-8') for i in temp]
	
	for i in ori_info:
		if i['date'] == temp[0] and i['name'] == temp[1]:
			
			# 原唱、演唱者、学校、这几个字段都可能为空
			title = u'%s%s《%s》' % (i['school'] + ' ' if '' != i['school'] else '',
			                        i['singer'] + ' ' if '' != i['singer'] else '', i['name'])
			
			# 如果歌名或原唱字段里有原创，或者原唱=翻唱，那这首歌是原创歌曲
			if     (i['original'] == u'原创') \
				or (i['singer'] == i['original'] and '' != i['original']) \
				or (u'原创' in i['name']):
				logging.info(u'原创歌曲：%s', str_dict_utf8(i))
			else:
				if '' != i['original']:
					title += u'(cover %s)' % (i['original'])
				logging.info(u'翻唱歌曲：%s', str_dict_utf8(i))
			file_info.append(title)
			return title
			
#	print '===not find=== %s === %s ' % (file_info[1], file_info[2])
	logging.info('===not find===：%s, %s' % (file_info[1], file_info[2]) )
	file_info.append(file_info[2])
	return file_info[2]
	
	

def upto_queue(file_info):
	logging.info('upto_queue: %s', str_dict_utf8(file_info))
	v = {
		"source_url": "",
		"publish_person": "不要音乐",
		"publish_img": "",
		"play_counts": random.randint(1000, 2000),
		"digg_count": random.randint(1000, 2000),
		"comment_counts": random.randint(1000, 2000),
		"share_count": random.randint(1000, 2000),
		"fav_counts": random.randint(1000, 2000),
		"publish_time": file_info[0],    #int(time.time()),
#		"publish_time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
		"title": file_info[3],
		"video_screen": "",
		"duration": "",
		"content": "http://bj.bcebos.com/musicwuliao/mv/buyaoyinyue/" + file_info[1],  # 是bos地址
	}
	
	tags = "音乐"
	Type = 2  # 资讯 - 1 ，视频 -2
	k = get_md5(v["title"])
	plat = 2401
	
	spider_data = {
		"spider_ref": {
			# 视频原始地址
			"sr_src_url": v["source_url"],
			# 视频平台 2401
			"sr_src_plat": plat,
			# 视频作者
			"sr_src_author": v["publish_person"],
			# 作者头像
			"sr_src_avator": v["publish_img"],
			# 视频浏览量
			"sr_src_review_num": str2int(nodata2null(v.get("play_counts"))),
			# 点赞量
			"sr_src_praise_num": nodata2null(v.get("digg_count")),
			# 评论量
			"sr_src_comment_num": nodata2null(v.get("comment_counts")),
			# 分享量
			"sr_src_share_num": nodata2null(v.get('share_count')),
			# 收藏量
			"sr_src_favorite_num": nodata2null(v.get("fav_counts")),
			# 发布时间
			"sr_src_publistime": v["publish_time"],
		},
		"video_info": {
			# 视频标题
			"vi_title": v["title"],
			# 视频原始链接（网页链接）
			"vi_video_url": v["source_url"],
			# 视频作者
			"vi_accountname": v["publish_person"],
			# 视频浏览量
			"vi_review_num": str2int(nodata2null(v.get("play_counts"))),
			# Type = 2
			"vi_source": Type,
			
		},
		
		"video_cover": [
			# 视频封面
			{"vc_url": v["video_screen"]}
		],
		"video_file": [{
			# 视频时长
			"vf_file_duration": v.get("duration"),
			# 是bos地址
			"vf_file_url": v["content"]
		}],
		"artist_ref": [
			# 相关艺人名字			# 视频Type 为2
			{"ar_artist_name": "", "ar_feed_type": Type}
		],
		"tag_ref": [
			# Type = 2  # 视频标签  # 视频标题
			{"tr_feed_type": Type, "tr_tag_name": tags, "tr_feed_title": v["title"]},
		]
	}
	
	info = {
		"function": 'add',
		"title": v['title'],
		"source": 2,  # 1为抓取, 2为对接
		"type": Type,  # 资讯 - 1 ，视频 -2
		"spider_id": k,
	#	"data": json.dumps(spider_data, ensure_ascii=False)
		"data": json.dumps(spider_data).decode('unicode-escape')
	}
	
	for k, v in info.items():
		logging.debug('%s=%s', k, v)

	
	url = "http://192.168.5.28:9090/feedapi/offline/queue"
#	url = "http://192.168.218.117:8188/feedapi/offline/queue"
	res = requests.post(url, data=info)
	logging.info("ret=%s, file=%s", res.text, file_info[3])
	print res.text
	return res.status_code == requests.codes.ok # 200:

def init_log(filename):
	LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
	DATE_FORMAT = "%Y%m%d %H:%M:%S"
	filename = filename+'.log'
	logging.basicConfig(filename=filename, level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
	logging.info('\n\n')
	logging.info('========STARTUP pid=[%d] cmd=%s ========' % (os.getpid(), sys.argv))
	

def main(bos_file, title_file):
	init_log(sys.argv[0])
	files_arr = get_bos_file(bos_file)
	title_arr = read_title_info(title_file)
	
	tlen = len(files_arr)
	stats = [0, 0, 0]
	for i in files_arr:
		change_file_title(i, title_arr)
		
		ret = upto_queue(i)
		stats[0] += 1
		if True == ret:
			stats[1] += 1
			print 'success! [%d:%d(%d/%d)] [%s]' % (tlen, stats[0], stats[1], stats[2], i[3])
		else:
			stats[2] += 1
			print '=== failed! [%d:%d(%d/%d)] [%s]' % (tlen, stats[0], stats[1], stats[2], i[3])

	log_str = 'BOS文件中，共 %d 个, 成功插入队列 %d 个, 失败 %d 个.' % (stats[0], stats[1], stats[2])
	logging.info(log_str)
	print log_str



if __name__ == "__main__":
	argc = len(sys.argv)
	if 2 == argc and ('-v' == sys.argv[1] or '-h' == sys.argv[1]):
		print '\n\t command line is like \'python %s bos_file title_file\'\n' % __file__
		print '\t\t bos_file : bos file to save, like \'bce bos ls bos:/musicwuliao/mv/buyaoyinyue > a\''
		print '\t\t title_file: 不要音乐MV成片信息表 - Liang.csv \n\n'
	else:
		bos_file   = sys.argv[1] if argc >= 2 else 'a'
		title_file = sys.argv[2] if argc >= 3 else '不要音乐MV成片信息表 - Liang.csv'
		title_file = unicode(title_file, 'utf-8')
		main(bos_file, title_file)

#
# 将bos上的文件，插入到转码队列中
#   文件存储在bos上，文件信息在本地文件中，例如a；
#   不能根据文件名转换title了，需要查询数据表，《不要音乐MV成片信息表 - Liang.csv》
#